function IceWarningManager(){
	var panelUtil;
	var panelId = "affectObjDiv";
	var protectUtil = new ProtectionStatusEvaluation();
	var icePanel;
	
	this.openMonitorDialog = function(data){
		var param = "?";
		for(var i in data){
			param += i+"="+data[i]+"&";
		}
		param = param.substring(0,param.length-1);
		var url = '/ui/map/dialog/icewarning/iceWarningMonitorDialog.xhtml';
		MyModal.showModal(url+param);
	};
	var voltageSelector = (function(){
		var voltages = {'all':'全部','499':'500kV及以上','220':'220kV','110':'110kV'};
		var selectVoltage = '499';
		var component;
		var listener;
		function getComponent(callback){
			component = $('<select style="width:70px;font-style:normal;"/>');
			for(var i in voltages){
				var s = voltages[i];
				var item = $('<option value="'+i+'" >'+s+'</option>');
				if(i == selectVoltage){
					item.attr('selected','selected');
				}
				component.append(item);
			}
			listener = callback;
			return component;
		};
		function getCurrentVoltage(){
			return selectVoltage;	
		}
		function getCurrentVoltageName(){
			return voltages[selectVoltage];	
		}
		function addListener(){
			component.on('change',function(params){
				selectVoltage = params.target.value;
				listener(selectVoltage);
			});
		}
		return {
			getComponent : getComponent,
			currentVoltage:getCurrentVoltage,
			currentVoltageName:getCurrentVoltageName,
			addListener:addListener,
		};
	})();
	var realTimeSelector = (function(){
		var defaultQueryGap;
		$.ajaxSettings.async = false;
		$.getJSON('/rest/ice/getIceWarningRealTimeDefaultQueryGap',function(result){
			defaultQueryGap = result;
		});
		$.ajaxSettings.async = true;
		var times = {43200:'12小时',86400:'24小时',172800:'48小时',259200:'72小时'};
		var defaultQueryHour = defaultQueryGap/3600;
		if(!times[defaultQueryGap]){
			times[defaultQueryGap] = defaultQueryHour+"小时";
		}
		var selectTime = defaultQueryGap;
		var component;
		var listener;
		function getComponent(callback){
			component = $('<select style="width:70px;font-style:normal;"/>');
			for(var i in times){
				var s = times[i];
				var item = $('<option value="'+i+'" >'+s+'</option>');
				if(i == selectTime){
					item.attr('selected','selected');
				}
				component.append(item);
			}
			listener = callback;
			return component;
		};
		function hide(){
			component.css('display','none');
		}
		function show(){
			component.css('display','block');
		}
		function getCurrentTime(){
			return selectTime;	
		}
		function getCurrentTimeName(){
			return times[selectTime];	
		}
		function addListener(){
			component.on('change',function(params){
				selectTime = params.target.value;
				listener(selectTime);
			});
		}
		function getQueryTime(){
			var times = [];
			var now = new Date();
			times[0] = DateUtil.date(now.setSeconds(now.getSeconds()-selectTime));
			times[1] = DateUtil.date(new Date());
			return times;
		}
		return {
			getComponent : getComponent,
			currentTime : getCurrentTime,
			currentTimeName : getCurrentTimeName,
			addListener : addListener,
			getQueryTime : getQueryTime,
			hide : hide,
			show : show,
		};
	})();
	function DetailIcon(){
		var detail;
		function getComponent(text,num,title,callback){
			detail = $('<span class="pull-right"></span>');
			detail.append(text);
			if(num>0){
				detail.append($('<span title="'+title+'" style="margin:3px;" class="badge bg-red">'+num+'</span>'));
			}
			detail.click(function(){
				callback();
			});
			return detail;
		}
		return {
			getComponent:getComponent,
		};
	};
	
	this.show = function(){
		clearAll();
//		if(!panelUtil){
			panelUtil = new PanelTablesUtil(panelId);
			icePanel = new IcePanel();
			panelUtil.setTitle("覆冰监测");
			panelUtil.setTitleTool(voltageSelector.getComponent(function(params){
				icePanel.changeVoltage(params);
			}));
			panelUtil.setTitleTool(realTimeSelector.getComponent(function(params){
				icePanel.changeTime(params);
			}));
			panelUtil.setTitleTool(queryDiv.getBtn);
			panelUtil.setInfo(queryDiv.getComponent);
			$.ajaxSettings.async = false;
			icePanel.show();
			$.ajaxSettings.async = true;
	//	}
		panelUtil.show();
		icePanel.drawMap();
		queryDiv.addListener();
		voltageSelector.addListener();
		realTimeSelector.addListener();
	};
	var queryDiv = (function() {
		var component,restore,query,error,minus,start,end;
		var queryGap;
		$.ajaxSettings.async = false;
		$.getJSON('/rest/ice/getIceWarningDefaultQueryGap',function(result){
			queryGap = result;
		});
		$.ajaxSettings.async = true;
		function getComponent(queryFunc){
			component = $('<div class="panel-collapse collapse" style="background:white" />');
			var now = new Date();
			var last = new Date(now.getTime()-Number(queryGap)*1000);
			var d = $('<div style="display:flex;"/>').appendTo(component);
			minus = $('<span class="btn btn-default" ><i class="fa fa-arrow-left"></i></span>').appendTo(d);
			start = $('<input type="text" class="form-control ice-warning-start"/>').appendTo(d);
			end = $('<input type="text" class="form-control ice-warning-end"/>').appendTo(d);
			restore = $('<span class="btn btn-default" ><i class="fa fa-repeat"></i></span>').appendTo(d);
			query = $('<span class="btn btn-default" >搜索</span>' ).appendTo(d);
			error = $('<div style="color:red" class="collapse">查询时间不得超过7天</div>').appendTo(component);
			
			start.val(getTimeStr(last));
			end.val(getTimeStr(now));
			
			return component;
		}
		function getBtn(){
			var btn = $('<span title="切换到查询模式" style="cursor:pointer;" class="fa fa-search" />');
			btn.click(function(){
				component.collapse('toggle');
				btn.toggleClass("fa fa-search");
				btn.toggleClass("fa fa-snowflake-o");
				if(btn.hasClass("fa-search")){
					btn.attr("title","切换到查询模式");
					realTimeSelector.show();
				}else{
					btn.attr("title","切换到实时数据");
					realTimeSelector.hide();
				}
			});
			return btn;
		}
		function getTimeStr(t){
			var m = t.getMonth() + 1 > 9 ? (t.getMonth() + 1) : ("0" + (t.getMonth() + 1));
			var d = t.getDate() > 9 ? t.getDate() : "0" + t.getDate();
			var timestr = t.getFullYear() + "-" + m + "-" + d;
			return timestr;
		}
		function addListener(){
			var now = new Date();
			var last = new Date(now.getTime()-7*24*3600*1000);
			var startTime = getTimeStr(last);
			var endTime = getTimeStr(now);
			restore.click(function(){
				start.val(getTimeStr(last));
				end.val(getTimeStr(now));
			});
			minus.click(function(){
				var str = start.val();
				var d = new Date(str);
				d.setTime(d.getTime()-24*3600*1000);
				start.val(getTimeStr(d));
			});
			query.click(function(){
				//将空的和不对的补齐
				var sta = start.val();
				var s = new Date(sta);
				if(!sta || s.toString() == 'Invalid Date'){
					sta = startTime;
					$('.ice-warning-start').val(startTime);
					s = new Date(sta);
				}
				var en = $('.ice-warning-end').val();
				var e = new Date(en);
				if(!en || e.toString() == 'Invalid Date'){
					en = endTime;
					$('.ice-warning-end').val(endTime);
					e = new Date(en);
				}
				//判断是否在允许范围内
				if(Math.abs(e.getTime()-s.getTime())>=8*24*3600*1000){
					error.collapse('show');
				}else{
					error.collapse('hide');
					startTime = sta;
					endTime = en;
					icePanel.query();
				}
			});
			start.datetimepicker({
				format: 'yyyy-mm-dd',
				minView : 2,
				autoclose : true,
				endDate : new Date(),
				todayHighlight : true,
			});
			end.datetimepicker({
				format: 'yyyy-mm-dd',
				minView : 2,
				autoclose : true,
				endDate : new Date(),
				todayHighlight : true,
				pickerPosition:'bottom-left',
			});
			component.on('shown.bs.collapse',function(param){
				if($(param.target).hasClass('panel-collapse')){
					icePanel.changeModel();
				}
			});
			component.on('hidden.bs.collapse',function(param){
				if($(param.target).hasClass('panel-collapse')){
					icePanel.changeModel();
				}
			});
		}
		function isShow(){
			return component.hasClass('in');
		}
		function getQueryTime(){
			return [$('.ice-warning-start').val(),$('.ice-warning-end').val()];
		}
		return {
			getComponent:getComponent,
			getBtn:getBtn,
			isShow:isShow,
			addListener:addListener,
			getQueryTime:getQueryTime
		};
	})();
	
	function IcePanel(){
		var body;
		var title;
		function update(){
			if(body){
				body.html('');
				title.html('');
			}else{
				body = panelUtil.getCollapsePanelEle("iceDataTable", "", true, null);
				title = body.prev('ul').find('li a');
			}
			if(queryDiv.isShow()){
				queryIce.show();
			}else{
				currentIce.show();
			}
			mapMonitors.update();
		}
		/**
		 * 需要外部的 电压等级
		 */
		var currentIce = (function(){
			function update(){
				drawLoading();
				$.getJSON("/rest/ice/getIceWarningTableData/"+voltageSelector.currentVoltage()
						+"/"+realTimeSelector.currentTime(),function(result){
					protectionStatus.show(result.protectionData);
					var td = result.tableData;
					for(var i in td){
						if(td[i][1]){
							td[i][1] = td[i][1].toFixed(2);
						}
					}
					drawPanelTable(td);
				});
			}
			function drawLoading(){
				var t = $('<span>覆冰线路</span>');
				title.html(t);
				body.html(PanelTablesUtil.getNoDataDiv('加载中...'));
			}
			function show(){
				update();
			}
			function drawPanelTable(data){
				var t = $('<span>覆冰线路</span>');
				var d = new DetailIcon();
				t.append(d.getComponent('明细',data.length,'线路数量',function(){
					var url = '/ui/map/dialog/icewarning/iceWarningListDialog.xhtml?voltage='
						+voltageSelector.currentVoltage()+'&second='+realTimeSelector.currentTime();
					MyModal.showModal(url,null,null,voltageSelector.currentVoltageName()+"线路覆冰监测");
				}));
				title.html(t);
				if(data && data.length>0){
					panelUtil.setDangerBox();
					var tb = $('<table class="table table-bordered table-hover" />');
					body.html(tb);
					iceTable.draw(data,['设备','覆冰厚度','上传时间'],tb,1);
				}else{
					body.html(PanelTablesUtil.getSmileInfo("无覆冰情况"));
				}
			}
			
			return {
				update:update,
				show:show
			};
		})();
		var protectionStatus = (function(){
			var title;
			var body;
			function update(protectionData){
				if(body){
					body.html('');
				}else{
					body = panelUtil.getCollapsePanelEle("iceProtection", "", false);
					title = body.prev('ul').find('li a');
				}
				var t = $('<span>保护状态评估</span>');
				title.html(t);
				protectUtil.setData(protectionData);
				body.append(protectUtil.getDescDiv());
				body.append(protectUtil.getTableDiv());
			}
			function drawLoading(){
				var t = $('<span>覆冰线路</span>');
				title.html(t);
				body.html(PanelTablesUtil.getNoDataDiv('加载中...'));
			}
			function show(protectionData){
				update(protectionData);
			}
			return {
				update:update,
				show:show
			};
		})();
		var queryIce = (function(){
			function update(){
				drawLoading();
				var times = queryDiv.getQueryTime();
				$.getJSON("/rest/ice/getIceWarningTableData/"+voltageSelector.currentVoltage()+'/'+times[0]+'/'+times[1],function(result){
					var td = result.tableData;
					for(var i in td){
						if(td[i][2]){
							td[i][2] = td[i][2].toFixed(2);
						}
					}
					drawPanelTable(td);
					protectionStatus.update(result.protectionData);
				});
			}
			function show(){
				update();
			}
			function drawLoading(){
				var t = $('<span>覆冰线路</span>');
				title.html(t);
				body.html(PanelTablesUtil.getNoDataDiv('加载中...'));
			}
			function drawPanelTable(data){
				var t = $('<span>覆冰线路</span>');
				var d = new DetailIcon();
				var times = queryDiv.getQueryTime();
				t.append(d.getComponent('明细',data.length,'线路数量',function(){
					var url = '/ui/map/dialog/icewarning/iceWarningListDialog.xhtml?voltage='
						+voltageSelector.currentVoltage()+'&start='+times[0]+'&end='+times[1];
					MyModal.showModal(url,null,null,voltageSelector.currentVoltageName()+"线路覆冰监测");
				}));
				title.html(t);
				if(data && data.length>0){
					panelUtil.setDangerBox();
					var tb = $('<table class="table table-bordered table-hover" />');
					body.html(tb);
					iceTable.draw(data,['设备','覆冰小时数','最大厚度'],tb,2);
				}else{
					body.html(PanelTablesUtil.getSmileInfo("无覆冰情况"));
				}
			}
			return {
				update:update,
				show:show
			};
		})();
		var iceTable = (function(){
			var ele;
			var table;
			
			function draw(data,columns,div,sortColumn){
				var opt = MyTable.getDefaultOptions();
				opt.scrollY = 300;
				opt.scrollCollapse = true;
				opt.ordering = true;
				opt.order = [[sortColumn,'desc']];
				ele = div?div:ele;
				table = new MyTable(ele,columns,opt).setDataSet(data).show();
				table.onRowClick(function(data){
					if(data){
						searchSelectObj(data[0],false,true,data[3]);
					}
				});
			}
			return {
				draw:draw
			};
		})();
		/**
		 * 需要外部的  电压等级  模式 查询时间
		 */
		var mapMonitors = (function(){
			var points = [];
			function show(){
				update();
			}
			function clear(){
				weatherLayer.removeFeatures(points);
				points = [];
			}
			function update(){
				clear();
				if(queryDiv.isShow()){
					var times = queryDiv.getQueryTime();
					$.getJSON("/rest/ice/getIceWarningMapData/"+voltageSelector.currentVoltage()+"/"+times[0]+"/"+times[1],function(result){
						if(queryDiv.isShow()){
							for(var i in result){
								if(result[i][5]){
									result[i][5] = result[i][5].toFixed(2);
								}
							}
							drawMapPoints(result,times);
						}
					});
				}else{
					var time = realTimeSelector.currentTime();
					$.getJSON("/rest/ice/getIceWarningMapData/"+voltageSelector.currentVoltage()+'/'+realTimeSelector.currentTime(),function(result){
						if(!queryDiv.isShow() && time == realTimeSelector.currentTime()){
							for(var i in result){
								if(result[i][5]){
									result[i][5] = result[i][5].toFixed(2);
								}
							}
							var times = realTimeSelector.getQueryTime();
							drawMapPoints(result,times);
						}
					});
				}
			}
			function drawMapPoints(data,times){
				clear();
				for(var i in data){
					var d = data[i];
					var value = {
							monitorId:d[0],
							lineName:d[3],
							towerNo:d[4]
					};
					if(times){
						value.startTime = times[0];
						value.endTime = times[1];
					}
					var width = $('#hiddenSpan').css('font-size','18px').html(d[5]+" ").width();
					var label = new SuperMap.Feature.Vector(
							new SuperMap.Geometry.Point(d[1],d[2]),
							null,
							{
								externalGraphic: "/resources/img/rectangle.png",
								label:d[5]+" ",
								fontWeight:700,
								fontSize:18,
								fontColor:'red',
								graphicOpacity:1,
								graphicWidth: width+2,
								graphicHeight: 20,
							}
					);
					label.data.type="iceMonitor";
					label.data.value = value;
					points.push(label);
					width = $('#hiddenSpan').css('font-size','14px').html(d[3]).width();
					$('#hiddenSpan').hide();
					var label2 = new SuperMap.Feature.Vector(
							new SuperMap.Geometry.Point(d[1],d[2]),
							null,
							{
								externalGraphic: "/resources/img/rectangle.png",
								label:d[3]+"",
								labelYOffset:-25,
								fontWeight:400,
								fontSize:14,
								fontColor:'blue',
								graphicOpacity:1,
								graphicWidth: width+2,
								graphicHeight: 20,
								graphicYOffset: 16,
							}
					);
					label2.data.type="iceMonitor";
					label2.data.value = value;
					points.push(label2);
				};
				weatherLayer.addFeatures(points);
				weatherLayer.setVisibility(true);
				selectFeature.activate();
			}
			return {
				show:show,
				clear:clear,
				update:update
			};
		})();
		return {
			changeModel:update,
			changeVoltage:update,
			changeTime:update,
			query:update,
			show:update,
			drawMap:mapMonitors.show
		};
	};
};